Public Class FormInsurance
    Dim myInsurance As DataTable
    Dim dView As DataView
    Private Sub initalizeData()
        Try
            loadData()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub loadData()
        'Get Data From Database
        Try
            myInsurance = Insurances.GetInsurance
            dgvInsurance.DataSource = myInsurance
        Catch ex As Exception

        End Try
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dbinterface.OpenConnection()
        initalizeData()
    End Sub
    Private Sub FormInsurance_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(dgvInsurance)
        btnAdd.Enabled = App.IsAuthorized(Permissions.Manage_Insurance_Add)
        btnUpdate.Enabled = App.IsAuthorized(Permissions.Manage_Insurance_Edit)
        btnDelete.Enabled = App.IsAuthorized(Permissions.Manage_Insurance_Delete)
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

        Dim i = dgvInsurance.Rows.Count.ToString
        i = i - 1
        dgvInsurance.CurrentCell = dgvInsurance.Rows(i).Cells(1)
        Dim frm = New FormInsuranceAED
        frm.ShowDialog()

    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        UpdateInsurance()
    End Sub
    Public Sub UpdateInsurance()
        'pass values from DataGridView to FormEmployeeAED when click Button Detail
        Try

            Dim row = Me.dgvInsurance.CurrentRow

            With FormInsuranceAED

                If row.Cells("INSURANCE_ID").Value Is Nothing Then
                    Exit Sub
                Else

                    .InId = row.Cells("INSURANCE_ID").Value
                    .cboEmp_Name.Text = row.Cells("EMPLOYEE_NAME").Value
                    .txtOccurDate.Text = row.Cells("OCCUR_DATE").Value
                    .txtOpenClaim.Text = row.Cells("OPEN_CLAIM").Value
                    .txtTypeOfClaim.Text = row.Cells("TYPE_OF_CLAIM").Value
                    .txtAmount.Text = row.Cells("AMOUNT").Value
                    .cboUserId.Text = row.Cells("MODIFY_BY").Value
                    .txtNote.Text = row.Cells("DESCRIPTION").Value
                    .txtCompany.Text = row.Cells("COMPANY").Value
                    .ShowDialog()
                End If

            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub dgvInsurance_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvInsurance.CellContentClick

    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Try
            'Check database connection
            Dim cn = Dbinterface.Connection
            If cn.State = ConnectionState.Closed Then

                Exit Sub

            End If

        Catch ex As Exception

        End Try
        Try
            FormDelete.ShowDialog()
            If FormDelete.DialogResult = Windows.Forms.DialogResult.Cancel Then
                Exit Sub
            End If


            'Remove DataGridView Row and Delete data in Database
            For Each row As DataGridViewRow In dgvInsurance.SelectedRows
                If row.Cells("INSURANCE_ID").Value = Nothing Then
                    Exit Sub
                End If
                Dim Ins As New InsuranceInfo
                Ins.InsuranceId = row.Cells("INSURANCE_ID").Value

                Insurances.DeleteInsurance(Ins)

                Me.dgvInsurance.Rows.Remove(row)
                dgvInsurance .CurrentCell = dgvInsurance .Rows(0).Cells(1)


            Next


        Catch ex As Exception

        End Try

    End Sub

    Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
        searchDataGridView()
    End Sub
    Public Sub searchDataGridView()
        'Allow user to search data in DataGridView by Name or Phone
        'CmdText.Parameters("@S") = "%" & txtSearch.Text & "%"
        Try
            Dim strFilter As String = "EMPLOYEE_NAME   Like  '%" & txtSearch.Text & "%'  Or COMPANY Like  '%" & txtSearch.Text & "%' And ACTIVE = 1"

            dView = New DataView(myInsurance, strFilter, "", DataViewRowState.CurrentRows)

            dgvInsurance.DataSource = dView


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub
End Class